﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Csla;

namespace RMSM
{
    [Serializable()]
    public class KBXQuangBOCollection : Csla.BusinessListBase<KBXQuangBOCollection, KBXQuangBO>
    {
        #region BindingList Overrides
        protected override object AddNewCore()
        {
            KBXQuangBO _KBXQuangBO = KBXQuangBO.NewChildKBXQuangBO();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_KBXQuangBO));
            Add(_KBXQuangBO);
            _KBXQuangBO.STT0 = this.Count;
            return _KBXQuangBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private KBXQuangBOCollection()
        {
        }

        public static KBXQuangBOCollection NewKBXQuangBOCollection()
        {
            return new KBXQuangBOCollection();
        }

        public static KBXQuangBO NewKBXQuangBO()
        {
            return KBXQuangBO.NewChildKBXQuangBO();
        }

        public static KBXQuangBOCollection GetKBXQuangBOCollection()
        {
            SPParam spParam = new SPParam("KBXQuangID", "ASC", "WHERE Deleted=0");
            return DataPortal.Fetch<KBXQuangBOCollection>(spParam);
        }

        public static KBXQuangBOCollection GetKBXQuangBOCollection(string where)
        {
            SPParam spParam = new SPParam("KBXQuangID", "ASC", "WHERE Deleted=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<KBXQuangBOCollection>(spParam);
        }

        public static KBXQuangBOCollection GetKBXQuangBOCollection(string where, string orderByColumn)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "KBXQuangID" : orderByColumn), "ASC", "WHERE Deleted=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<KBXQuangBOCollection>(spParam);
        }

        public static KBXQuangBOCollection GetKBXQuangBOCollection(string where, string orderByColumn, string orderByExpression)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "KBXQuangID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), "WHERE Deleted=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<KBXQuangBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            RaiseListChangedEvents = false;
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBXQuangBO_SelectBOCollection");
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                int stt = 0;
                while (dr.Read())
                {
                    KBXQuangBO objKBXQuangBO = KBXQuangBO.GetChildKBXQuangBO(dr);
                    objKBXQuangBO.STT0 = ++stt;
                    this.Add(objKBXQuangBO);
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
            RaiseListChangedEvents = false;
            foreach (KBXQuangBO child in this)
            {
                if (child.IsNew && !StaticUtil.GetBool(child.Deleted))
                    child.Insert();
                else
                    child.Update();
            }
            RaiseListChangedEvents = true;
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public KBXQuangBO GetKBXQuangBO(int _KBXQuangID)
        {
            KBXQuangBO returnObject = null;
            foreach (KBXQuangBO _KBXQuangBO in this)
            {
                if (_KBXQuangBO.KBXQuangID == _KBXQuangID)
                {
                    returnObject = _KBXQuangBO;
                    break;
                }
            }
            return returnObject;
        }

        public int GetIndex(int _KBXQuangID)
        {
            if (_KBXQuangID == null)
                return -1;

            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].KBXQuangID == _KBXQuangID)
                    return i;

            return -1;
        }

        public void Remove(int KBXQuangID)
        {
            foreach (KBXQuangBO _KBXQuangBO in this)
            {
                if (_KBXQuangBO.KBXQuangID == KBXQuangID)
                {
                    try
                    {
                        this.Remove(_KBXQuangBO);
                    }
                    catch { }
                    break;
                }
            }
        }
        #endregion Business Method
        //==========================================================================
    }
}
